package com.itcodai.controller.course12;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import javax.servlet.http.HttpServletRequest;

/**
 * @author c-fanqj
 * @date 2023/4/5
 * @apiNote 使用ServletRequestListener获取访问信息
 * 3、监听客户端请求Servlet Request对象
 */
//@Component
public class MyServletRequestListener implements ServletRequestListener {

    private final Logger logger =
            LoggerFactory.getLogger(this.getClass());

    @Override
    public void requestInitialized(ServletRequestEvent sre) {
        HttpServletRequest request =
                (HttpServletRequest) sre.getServletRequest();
        logger.info("session id为：{}", request.getRequestedSessionId());
        logger.info("request url为：{}", request.getRequestURL());
        request.setAttribute("name", "jiejie");
    }

    @Override
    public void requestDestroyed(ServletRequestEvent sre) {
        logger.info("request end");
        HttpServletRequest request =
                (HttpServletRequest) sre.getServletRequest();
        logger.info("request域中保存的name值为：{}",
                request.getAttribute("name"));
    }


}
